<template>
  <div class="app-container">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="app-header">
        <div class="header-title">臻品甄选用户系统</div>
        <div class="header-actions">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="32" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
              <span class="user-name">管理员</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside width="220px" class="app-sidebar">
          <el-menu
              :default-active="activeMenu"
              class="el-menu-vertical"
              :unique-opened="true"
              router
              @select="handleSelect"
          >
            <!-- 动态菜单部分 -->
            <template v-for="menu in menuList" :key="menu.id">
              <el-sub-menu v-if="menu.children?.length" :index="menu.path">
                <template #title>
                  <el-icon><component :is="menu.icon || 'Menu'" /></el-icon>
                  <span>{{ menu.name }}</span>
                </template>
                <el-menu-item
                    v-for="child in menu.children"
                    :key="child.id"
                    :index="child.path"
                >
                  <el-icon><component :is="child.icon || 'Menu'" /></el-icon>
                  <span>{{ child.name }}</span>
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="menu.path">
                <el-icon><component :is="menu.icon || 'Menu'" /></el-icon>
                <span>{{ menu.name }}</span>
              </el-menu-item>
            </template>
          </el-menu>
        </el-aside>

        <!-- 主内容区 -->
        <el-main class="app-main">
          <RouterView/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'
import {
  Menu,
  Setting,
  User,
  Key,
  Location,
  Document,
  Goods,
  ShoppingCart
} from '@element-plus/icons-vue'

const route = useRoute()
const menuList = ref([])

// 当前激活菜单
const activeMenu = computed(() => {
  return route.path
})

// 获取动态菜单
const fetchMenuByRole = async () => {
  try {
    const response = await axios.get('/api/menu/menuUser/tree')
    menuList.value = response.data.map(menu => ({
      ...menu,
      icon: getIconComponent(menu.icon)
    }))
  } catch (error) {
    console.error('获取菜单失败', error)
  }
}

// 图标映射
const getIconComponent = (iconName) => {
  const iconMap = {
    goods: Goods,
    order: ShoppingCart,
    system: Setting,
    user: User,
    default: Menu
  }
  return iconMap[iconName] || iconMap.default
}

// 菜单选中事件
const handleSelect = (index) => {
  console.log('菜单选中:', index)
}

onMounted(() => {
  fetchMenuByRole()
})
</script>

<style scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #409EFF;
  color: white;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-title {
  font-size: 18px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-name {
  margin-left: 10px;
}

.app-sidebar {
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
  transition: width 0.3s;
}

.el-menu-vertical {
  border-right: none;
  height: 100%;
}

.app-main {
  background-color: #f0f2f5;
  padding: 20px;
  height: calc(100vh - 60px);
  overflow: auto;
}

.el-menu-item,
.el-sub-menu__title {
  height: 48px;
  line-height: 48px;
}

.el-menu-item.is-active {
  background-color: #ecf5ff !important;
  color: #409EFF;
}
</style>